﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using ihongma.BLL;
using System.Collections.Generic;
using ihongma.Common;
using System.Web.Script.Serialization;
namespace ihongma.Web.tools
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class tags : IHttpHandler
    {
        BLL.Tags bll = new ihongma.BLL.Tags();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = HttpContext.Current.Request["action"].ToString();
            string ret=string.Empty;
           
            switch (action)
            {
                case "add": //加载频道管理菜单
                    ret = add(DTRequest.GetFormString("name")).ToString();
                    break;
                case "update": //加载插件管理菜单
                    ret = update(DTRequest.GetQueryInt("id"), DTRequest.GetQueryString("age")).ToString();
                    break;
                case "delete": //验证频道名称是否重复
                    ret = delete(DTRequest.GetQueryInt("id")).ToString();
                    break;
                case "list": //删除系统模型菜单
                    ret =getlist();
                    break;
            }
          

           
            context.Response.Write(ret);
        }
        private bool add(string name)
        {
            ihongma.Model.Tags Model=new ihongma.Model.Tags();
            Model.hhits = 1;
            Model.qhits = 1;
            Model.name = name;
            Model.Createdtime = DateTime.Now;
           return bll.Add(Model);
        }
        private bool update(int id,string age)
        {
            bool ret=false;
            if (age == "h")
            {
                 bll.UpdateField(id, "hhits=hhits+1");
            }
            else
            {
                 bll.UpdateField(id, "qhits=qhits+1");
            }
            return ret;
        }
        private string getlist()
        {
            JavaScriptSerializer jserializer = new JavaScriptSerializer();
            List<ihongma.Model.Tags> taglist = new List<ihongma.Model.Tags>();
           DataTable dt=bll.GetAllList().Tables[0];
           taglist = bll.DataTableToList(dt);
           return jserializer.Serialize(taglist);
        }
        //private string getlist(string str)
        //{
        //    JavaScriptSerializer jserializer = new JavaScriptSerializer();
        //    List<ihongma.Model.Tags> taglist = new List<ihongma.Model.Tags>();
           
        //    taglist = bll.GetModelList("id in ("+str+")");
        //    return jserializer.Serialize(taglist);
        //}
        private bool delete(int id)
        {
            bool ret = false;
            ret = bll.Delete(id);
            return ret;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
